<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/8/9
 * Time: 17:22
 */

namespace Biz\module\Classes;

use Biz\pockets\constInterface\ActivityInfo;
use Biz\service\Goods as sGoods;
use Biz\service\ShopInfo as sShopInfo;
use Biz\tools\IsTestShop;
use Doraemon\amqp\Queue;
use Doraemon\service\User as sUser;
use Doraemon\tools\HhzEnv;
use Doraemon\model\Member as mMember;

class OrderWXNotice implements ActivityInfo
{
    const  NOTICE_CATEGORYS = [1];

    public static function noticeWechatNewOrder($orderShops, $orderGoodsList)
    {
        if (HhzEnv::isProductionWithoutApi2()) {
            $goodsList = sGoods::Goods(array_column($orderGoodsList, 'goods_id'), sGoods::FIELDS_GROUP_ORDER);
            foreach ($orderShops as $orderInfo) {
                if (!IsTestShop::isTestShop($orderInfo['shop_id'])) {
                    $noticeGoodsTitle = [];
                    foreach ($orderGoodsList as $orderGoods) {
                        if ($orderGoods['order_no'] == $orderInfo['order_no'] && in_array($goodsList[$orderGoods['goods_id']]['category_id'], self::NOTICE_CATEGORYS)) {
                            $noticeGoodsTitle[] = $goodsList[$orderGoods['goods_id']]['title'];
                        }
                        if ($orderGoods['promo_type'] == self::ACTIVITY_TYPE_TUAN) {
                            $userInfo = sUser::getUser($orderInfo['uid']);
                            Queue::insertQueue("queue/Wechatnotice/toTag", [
                                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_BUSINESS_ORDER,
                                "content" => '【好好住团购】商品：' . $goodsList[$orderGoods['goods_id']]['title'] . "被住友 " . $userInfo['nick'] . " 拍下啦~"
                            ], Queue::QUEUE_BIZ);
                        }
                    }
                    if ($noticeGoodsTitle) {
                        $userInfo = sUser::getUser($orderInfo['uid']);
                        $shopInfo = sShopInfo::getShopInfo($orderInfo['shop_id']);
                        Queue::insertQueue("queue/Wechatnotice/toTag", [
                            "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_BUSINESS_FURNITURE_ORDER,
                            "content" => '【好好住商城】' . $shopInfo['name'] . "的 " . implode("、", $noticeGoodsTitle) . " 等商品已由住友 " . $userInfo['nick'] . " 支付完毕，订单号：" . $orderInfo['order_no'] . "请尽快联系用户确认收货信息。"
                        ], Queue::QUEUE_BIZ);
                    }
                }
            }
        }
    }

    public static function jiexuanNoticeWeChatNewOrder($orderInfoList, $orderGoodsList)
    {
        /*
         * 【theParagraph节选】雅痞YuppieLife（商家名称）的 帆船成人系列懒人豆袋沙发（商品名称）；ziinlife吱音的 蒙德里安床头柜边柜实木多功能收纳柜储物柜商品已由手机尾号为xxxx （该用户手机尾号后四位）支付，实付总金额xxxx元，订单号：O190320-2DTF1N-4X9Q9Z8-1。
         */

        $goodsListByOrderNo = [];
        foreach ($orderGoodsList as $value){
            $goodsListByOrderNo[$value['order_no']][] = $value;
        }

        $uid = array_shift(array_column($orderInfoList,'uid'));

        $userInfo = mMember::getUserInfo($uid);

        foreach ($orderInfoList as $orderInfo) {
            $message = "【节选新订单提醒】";

            $shopInfo = sShopInfo::getShopInfo($orderInfo['shop_id']);

            foreach ($goodsListByOrderNo[$orderInfo['order_no']] as $orderGoods) {
                $message .= sprintf("%s 的 %s ;",$shopInfo['name'],$orderGoods['title']);
            }

            $message .= sprintf("已由手机尾号为%s (%s)支付，",substr($orderInfo['consignee_phone'],-4),$userInfo['nick']);
            $message .= sprintf("实付总金额为%s元，",$orderInfo['pay_amount']);
            $message .= sprintf("订单号：%s",$orderInfo['order_no']);

            Queue::insertQueue("queue/Wechatnotice/toTag", [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_JIEXUAN_NEW_ORDER,
                "content" => $message
            ], Queue::QUEUE_BIZ);
        }

    }
}